﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Text;
using App.Core.Mail;

public partial class Pages_Public_ContactUs : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindUserInfo();
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void BindUserInfo()
    {
        if (SessionCache.CurrentUser != null)
        {
            if (SessionCache.CurrentUser.Contact == null)
            {
                txtName.Text = SessionCache.CurrentUser.Customer.Name;
                txtEmail.Text = SessionCache.CurrentUser.LoginName;
            }
            else
            {
                txtName.Text = SessionCache.CurrentUser.Contact.Name;
                txtEmail.Text = SessionCache.CurrentUser.Contact.Email;
            }
        }
    }
    protected void SendMessage()
    {
        BtcHousingEntities context = new BtcHousingEntities();

        StringBuilder emailTemplate = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("Dear {0} Admin,<br/>", ConfigReader.ProviderCompany);
        sb.AppendFormat("An user has sent you the following message<br /><br />");        
        sb.AppendFormat("{0}", WebUtil.FormatText(txtMessage.Text));

        emailTemplate.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());
        emailTemplate.Replace(AppConstants.ETConstants.MESSAGE, sb.ToString());
        String fromEmail = txtEmail.Text.Trim();
        //String toEmail = ConfigReader.AdminEmail;
        String toEmail = ConfigReader.InfoEmail;
        String subject = String.Format("Feedback Message from {0}", ConfigReader.ProviderCompany);
        try
        {
            MailManager.SendMail(toEmail, String.Empty, String.Empty, fromEmail, subject, emailTemplate.ToString());
            WebUtil.ShowMessageBox(divMessage, String.Format("Your Message has been sent to {0}", ConfigReader.ProviderCompany), false);
        }
        catch
        {
            //BtcHousingEntities context = new BtcHousingEntities();
            EmailMessage failedMessage = new EmailMessage();
            failedMessage.Subject = subject;
            failedMessage.FromEmail = fromEmail;
            failedMessage.ToEmail = toEmail;
            failedMessage.Message = emailTemplate.ToString();
            failedMessage.IsSent = false;
            failedMessage.Created = DateTime.Now;
            failedMessage.AttemptedOn = DateTime.Now;
            context.AddToEmailMessages(failedMessage);
            context.SaveChanges();
            WebUtil.ShowMessageBox(divMessage, String.Format("Sorry! Your Message cannot be sent to {0}. An Internal Error occured and our technical team has been notified about this error.", ConfigReader.ProviderCompany), true);
        }
    }
    protected void btnSendMessage_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SendMessage();
        }
    }
}